Page({

  /**
   * 页面的初始数据
   */
  data: {
      src: '../../images/beijin.png', 
      isPlay: false,
      isSingle: false,
      videoSrc: 'http://music.163.com/song/media/outer/url?id=1951069525.mp3',
      name: '精卫',
      value: 0,
      maxValue: 0,
      currentTime: '0:00',
      duration: '0:00',
      index: 0,
      currentInd: 0,
      showAlert: false,
      videos: [
      {name:'精卫', singer: '30年前，50年后', videoSrc:'http://music.163.com/song/media/outer/url?id=1951069525.mp3'},
      {name:'可能', singer: '程响', videoSrc: 'http://music.163.com/song/media/outer/url?id=1950343972.mp3'},
      {name:'雪 Distance', singer: 'Capper / 罗言', videoSrc: 'http://music.163.com/song/media/outer/url?id=2026224214.mp3'},
      {name:'我记得', singer: '赵雷', videoSrc: 'http://music.163.com/song/media/outer/url?id=1974443814.mp3'},
      {name:'梦醒', singer: 'handsome lau / HYPEEZY / 冯泳', videoSrc: 'http://music.163.com/song/media/outer/url?id=1395222212.mp3'},
      {name:'唯一', singer: '告五人', videoSrc: 'http://music.163.com/song/media/outer/url?id=1807799505.mp3'},
      {name:'罗生门（Follow）', singer: '梨冻紧 / Wiz_H张子豪', videoSrc: 'http://music.163.com/song/media/outer/url?id=1456890009.mp3'},
      {name:'还是会想你', singer: '林达浪 / h3R3', videoSrc: 'http://music.163.com/song/media/outer/url?id=1827600686.mp3'},
      {name:'爱人错过', singer: '告五人', videoSrc: 'http://music.163.com/song/media/outer/url?id=1368754688.mp3'},
      {name:'达尔文', singer: '林俊杰', videoSrc: 'http://music.163.com/song/media/outer/url?id=2019573476.mp3'},
      {name:'乐园', singer: '沧桑CangSang / 虎皮蛋 / 曲甲', videoSrc: 'http://music.163.com/song/media/outer/url?id=2021379728.mp3'},
      {name:'落泪', singer: '康熙 / TOYOKI', videoSrc: 'http://music.163.com/song/media/outer/url?id=2021434589.mp3'},
      {name:'谁', singer: '王贰浪', videoSrc: 'http://music.163.com/song/media/outer/url?id=2026812798.mp3'},
      {name:'我的爱如烈火', singer: 'xxxmiracle / mok', videoSrc: 'http://music.163.com/song/media/outer/url?id=2024654768.mp3'},
      ]
  },
  // 格式化时间
  timeFormat(e) {
      let time = Math.floor(e)
      const minutes = Math.floor(time / 60)
      const seconds = time % 60
      const result = `${this.padTo2Digits(minutes)}:${this.padTo2Digits(seconds)}`
      return result
  },
  padTo2Digits(num) {
      return num.toString().padStart(2, '0');
  },
  // 初始化音频
  initialAudio() {
      let audioCtx = this.audioCtx
      audioCtx.src = this.data.videoSrc
      audioCtx.loop = this.data.isSingle
      audioCtx.onEnded(() => {
          this.next()
      })
      audioCtx.onCanplay(() => {
      audioCtx.duration
      audioCtx.currentTime
      setTimeout(() => {
          this.setData({
              value: audioCtx.currentTime,
              maxValue: audioCtx.duration,
              currentTime: this.timeFormat(audioCtx.currentTime),
              duration: this.timeFormat(audioCtx.duration)
          })
      },1000)
      })
      audioCtx.onTimeUpdate(() => {
          this.setData({
              value: audioCtx.currentTime,
              maxValue: audioCtx.duration,
              currentTime: this.timeFormat(audioCtx.currentTime),
              duration: this.timeFormat(audioCtx.duration)
          })
      })
  },
  toTime(e) {
      console.log(e.detail);
      let time = e.detail.value
      this.audioCtx.seek(time)
  },
  isSingle() {
      let isSingle = this.data.isSingle
      this.setData({
          isSingle: !isSingle
      })
      this.audioCtx.loop = this.data.isSingle
  },
  isPlay() {
      let isPlay = this.data.isPlay
      if(!isPlay) {
      this.audioCtx.src = this.data.videoSrc
      this.audioCtx.play()
      } else {
      this.audioCtx.pause()
      }
      this.setData({
          isPlay: !isPlay,
      })
  },
  showActionSheet() {
      let list = this.data.videos.map(x => {return {"name":x.name,"singer": x.singer}})
      console.log(list);
      // let that = this
      this.setData({
        showAlert: true,
        list
      })
  },
  next() {
      let index = this.data.index + 1
      let list = this.data.videos
      let isPlay = this.data.isPlay
      let videoSrc = ''
      let name = ''
      if(index>=list.length) {
          index = 0
      }
      videoSrc = list[index].videoSrc
      name = list[index].name
      // this.audioCtx.stop()
      if(isPlay) {
          isPlay = !isPlay
      }
      this.setData({
          isPlay,
          index,
          currentInd: index,
          videoSrc: videoSrc,
          name: name
      })
      this.isPlay()
  },
  pre() {
      let index = this.data.index - 1
      let list = this.data.videos
      let isPlay = this.data.isPlay
      let videoSrc = ''
      let name = ''
      if(index<0) {
          index = list.length - 1 
      }
      videoSrc = list[index].videoSrc
      name = list[index].name
      this.audioCtx.stop()
      if(isPlay) {
          isPlay = !isPlay
      }
      this.setData({
          isPlay,
          index,
          currentInd: index,
          videoSrc: videoSrc,
          name: name
      })
      this.isPlay()
  },

  change(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.ind);
    let index = e.currentTarget.dataset.ind
    this.setData({
        currentInd: index,
        index: index,
        name: this.data.videos[index].name,
        videoSrc: this.data.videos[index].videoSrc,
        isPlay: false,
        showAlert: false
    })
    this.audioCtx.stop()
    this.isPlay()
  },
  closeAlert() {
    this.setData({
      showAlert: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.audioCtx = wx.createInnerAudioContext()
      this.initialAudio()
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
  })